define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'customer/unit_customer/index' + location.search,
                    add_url: 'customer/unit_customer/add',
                    edit_url: 'customer/unit_customer/edit',
                    del_url: 'customer/unit_customer/del',
                    multi_url: 'customer/unit_customer/multi',
                    import_url: 'customer/unit_customer/import',
                    table: 'unit_customer',
                }
            });

            var table = $("#table");
            table.on('post-body.bs.table', function (e, settings, json, xhr) {
                $(".btn-editone").data("area",[ '100%' , '100%']);
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title'), operate: 'LIKE'},
                        //{field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'contacts', title: __('Contacts'), operate: 'LIKE'},
                        /*{field: 'admin.nickname', title: __('Principal'), operate: 'LIKE'},*/
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        index1: function () {
            window.myevents = {
                'click .btn-select':function(e,value,row,index){
                    // window.parent.$('#c-unit_customer_id').val(row.id);
                    // window.parent.$('#c-unit_customer_title').val(row.title);
                    window.parent.$('#c-title').val(row.title);
                    window.parent.$('#c-business').val(row.business);
                    window.parent.$('#c-brief_introduction').val(row.brief_introduction);
                    window.parent.$('#c-city').val(row.city);
                    window.parent.$('#c-address').val(row.address);
                    window.parent.$('#c-mobile').val(row.mobile);
                    window.parent.$('#c-nature').val(row.nature);
                    window.parent.$('#c-contacts').val(row.contacts);
                    window.parent.$('#c-sex_data').val(row.sex_data);
                    window.parent.$('#c-department').val(row.department);
                    window.parent.$('#c-job_title').val(row.job_title);
                    window.parent.$('#c-channel_type').val(row.channel);
                    window.parent.$('#c-mobild').val(row.mobild);
                    window.parent.$('#c-email').val(row.email);
                    window.parent.$('#c-birthday_time').val(row.birthday_time);
                    window.parent.$('#c-activity_start_time').val(row.activity_start_time);
                    window.parent.$('#c-activity_end_time').val(row.activity_end_time);
                    window.parent.$('#c-activity_number').val(row.activity_number);
                    window.parent.$('#c-man_number').val(row.man_number);
                    window.parent.$('#c-woman_number').val(row.woman_number);
                    window.parent.$('#c-age_composition').val(row.age_composition);
                    window.parent.$('#c-participant').val(row.participant);
                    window.parent.$('#c-service_demand').val(row.service_demand);
                    window.parent.$('#c-channel').val(row.channel);
                    window.parent.$('#c-activity_needs').val(row.activity_needs);
                    window.parent.$('#c-activity_goal').val(row.activity_goal);
                    window.parent.$('#c-activity_theme').val(row.activity_theme);
                    window.parent.$('#c-management').val(row.management);
                    window.parent.$('#c-user_remark').val(row.user_remark);

                    var index = parent.layer.getFrameIndex(window.name);
                    Fast.api.close(row);
                    //console.log(parent.layer.close(index));
                }
            }

            function myformatter(){
                return [
                    '<button class="btn btn-xs btn-primary btn-select"  type="button">选择</button>'
                ].join("");
            }
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'customer/unit_customer/index1' + location.search,
                    add_url: 'customer/unit_customer/add',
                    edit_url: 'customer/unit_customer/edit',
                    del_url: 'customer/unit_customer/del',
                    multi_url: 'customer/unit_customer/multi',
                    import_url: 'customer/unit_customer/import',
                    table: 'unit_customer',
                }
            });

            var table = $("#table");
            table.on('post-body.bs.table', function (e, settings, json, xhr) {
                $(".btn-editone").data("area",[ '100%' , '100%']);
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title'), operate: 'LIKE'},
                        {field: 'contacts', title: __('Contacts'), operate: 'LIKE'},
                        {field: 'admin.nickname', title: __('Principal'), operate: 'LIKE'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table,events:myevents,formatter:myformatter}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();

        },
        edit: function () {
            Controller.api.bindevent();

        },
        api: {
            bindevent: function () {
                $('#level').on('click',function(){
                    Fast.api.open(Fast.api.fixurl('customer/unit_customer/index1'),'上级客户',{
                        area:["60%","70%"],
                    })

                })

                $(document).on('click','.types',function () {
                    var that = this
                    var type = $(that).data('type');
                    var search = $(that).data('search');

                    var val = $('#c-'+type+'_type').val();
                    var url = 'category/look';
                    if(search=='所属行业')
                    {
                        url = 'category/industry';
                    }
                    var url = url+'?type=' +  type+'&val='+val+'&search='+search;
                    Fast.api.open(Fast.api.fixurl(url),search,{
                        area:["60%","70%"],
                        callback:function(res){
                            console.log('res'+res)
                            $(that).val(res)
                            try {
                                if(typeof  callback === 'function'){
                                    callback.call(that,res)
                                }
                            }catch (e) {

                            }
                        }
                    })
                })
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
